import { defineStore } from "pinia";
import { computed, ref } from "vue";
import axios from "axios"





const listStore = defineStore("list",()=>{

    const bottom = ref(true) //底部导航栏
    const mainDataList = ref([])

    const finished = ref(false); // list数据加载判断
    const loading = ref(false);
    const pageNum = ref(1)
    const total = ref(0)
    const cinemaList = ref([])
    const ticketFlag = ref(1)
    const cityList = ref([])

    const cityId = ref(110100) // 城市的id
    const cityName = ref("北京") // 城市名称
    const detailList = ref([]) 

    const mobile = ref(null)  // 用户输入的手机号
    const status = ref()  //请求完后端返回状态码信息
    const msg = ref()  //返回是否成功的信息
    const smsCode = ref(null) //验证码
    const dataList = ref(null)  // 用户点击登录成功获取的用户信息以及token

    const cinemasIds = ref({       //请求影院详细信息的参数
        "cityId":"",
        "cinemaIds":""
    })   
    const cinemasDetailList = ref([])  //影片影院的详细信息
    const filmId = ref()  //保存用户点击的影片id
    const cinemaId = ref() //保存用户点击的影院id
    const cinemaFilmData = ref()  //保存点击影院的信息
    const cinemaFilmLists = ref()  //影院中影片详细信息
    const toListOrCinemas = ref(true)


    // 获取电影数据列表
    const getList = (async(cityId,pageNum,type)=>{
        
            const res =  await axios({  
                url:`https://m.maizuo.com/gateway?cityId=${cityId}&pageNum=${pageNum}&pageSize=10&type=${type}&k=4231560`,
                headers:{
                    'X-Client-Info':'{"a":"3000","ch":"1002","v":"5.2.1","e":"16923257271850709997780993","bc":"110100"}',
                    'X-Host':'mall.film-ticket.film.list'
                }
            })
            mainDataList.value = res.data.data.films
            total.value = res.data.data.total
       
    })

    // 获取影院数据列表
    const getCinemaList = (async (cityId,ticketFlag) =>{
      const res =   await axios({
            url:`https://m.maizuo.com/gateway?cityId=${cityId}&ticketFlag=${ticketFlag}&k=4158781`,
            headers:{
                'X-Client-Info':'{"a":"3000","ch":"1002","v":"5.2.1","e":"16923257271850709997780993","bc":"110100"}',
                'X-Host':'mall.film-ticket.cinema.list'
            }
        })
        // console.log(res.data.data.cinemas);
        cinemaList.value = res.data.data.cinemas
    })

    // 获取城市数据列表
    const getCityList = (async () => {
        const res =   await axios({
            url:`https://m.maizuo.com/gateway?k=5700024`,
            headers:{
                'X-Client-Info':'{"a":"3000","ch":"1002","v":"5.2.1","e":"16923257271850709997780993","bc":"110100"}',
                'X-Host':'mall.film-ticket.city.list'
            }
        })
        console.log(res);
        cityList.value =  res.data.data.cities
    })

    // 获取电影详情数据
    const getDetailList = (async (filmId) => {
        const res =   await axios({
            url:`https://m.maizuo.com/gateway?filmId=${filmId}&k=1397748`,
            headers:{
                'X-Client-Info':'{"a":"3000","ch":"1002","v":"5.2.1","e":"16923257271850709997780993","bc":"110100"}',
                'X-Host':'mall.film-ticket.film.info'
            }
        })
        detailList.value =  res.data.data.film
        console.log(detailList.value);
    })

    // 输入手机号点击获取验证码发送请求
    const getTrue = (async (mobile) => {
        const res =   await axios({
            url:`https://m.maizuo.com/gateway`,
            method:"post",
            data:{
                    imgCode:"",
                    imgKey:"",
                    mobile:mobile,
                    type:"1"

            },
            headers:{
                'X-Client-Info':'{"a":"3000","ch":"1002","v":"1.0.0","e":"16923257271850709997780993"}',
                'X-Host':'mall.user.sms-code.send'
            }
        })
        console.log(res.data.msg);
        status.value = res.data.status
        msg.value = res.data.msg
        console.log(status.value);
    })

    // 点击登录按钮发送请求
    const getSms = (async (mobile,smsCode) => {
        const res =   await axios({
            url:`https://m.maizuo.com/gateway`,
            method:"post",
            data:{
                    extra:{},
                    imgCode:"",
                    imgKey:"",
                    mobile:mobile,
                    smsCode:smsCode

            },
            headers:{
                'X-Client-Info':'{"a":"3000","ch":"1002","v":"1.0.0","e":"16923257271850709997780993"}',
                'X-Host':'mall.user.sms-code-login'
            }
        })
        console.log(res);
        status.value = res.data.status
        msg.value = res.data.msg
        
        // 根据后端返回的信息判断是否登录成功

        if(msg.value == "ok") {
            // 把返回的信息保存起来
            dataList.value = res.data.data
            console.log(dataList.value);
        }
       
        
    })

    // 获取有该电影的影院cinemasId
    const getCinamesIds = (async (filmId,cityId) => {
        const res =   await axios({
            url:`https://m.maizuo.com/gateway/?filmId=${filmId}&cityId=${cityId}&k=549658`,
            headers:{
                'X-Client-Info':'{"a":"3000","ch":"1002","v":"5.2.1","e":"16923257271850709997780993","bc":"310100"}',
                'X-Host':'mall.film-ticket.cinema.film-show-cinema'
            }
        })
        // 整理得到的数据
        res.data.data.cinemaExtendList.forEach(item => {
        //   console.log(item.cinemaId);
          cinemasIds.value["cinemaIds"] += item.cinemaId + ","
        });
       cinemasIds.value["cinemaIds"] = cinemasIds.value["cinemaIds"].slice(0,-1)

       
    })

    // 通过获得到的cinemasId去获取影院
    const getCinamesIdList = (async () => {
        const res =   await axios.post(`https://m.maizuo.com/gateway?k=7786042`,cinemasIds.value,{
            headers:{
                'X-Client-Info':'{"a":"3000","ch":"1002","v":"5.2.1","e":"16923257271850709997780993","bc":"310100"}',
                'X-Host':'mall.film-ticket.cinema.batch-cinema'
            }
        })
       console.log(res);
        cinemasDetailList.value = res.data.data.cinemas
        console.log(cinemasDetailList.value);
    })

    // 点击不同的时间，重新获取影院信息
    const getDateCinamesIdList = (async () => {
        const res =   await axios.post(`https://m.maizuo.com/gateway?k=2910965`,cinemasIds.value,{
            headers:{
                'X-Client-Info':'{"a":"3000","ch":"1002","v":"5.2.1","e":"16923257271850709997780993","bc":"110100"}',
                'X-Host':'mall.film-ticket.cinema.batch-cinema'
            }
        })
        cinemasDetailList.value = null
        cinemasDetailList.value = res.data.data.cinemas
    })

     // 获取用户点击影院的影院名称及其他信息
     const getCinemasFilm = (async (cinemaId) => {
        const res =   await axios({
            url:`https://m.maizuo.com/gateway/?cinemaId=${cinemaId}&k=5329412`,
            headers:{
                'X-Client-Info':'{"a":"3000","ch":"1002","v":"5.2.1","e":"16923257271850709997780993","bc":"110100"}',
                'X-Host':'mall.film-ticket.cinema.info'
            }
        })
        cinemaFilmData.value = res.data.data.cinema
        console.log(cinemaFilmData.value);
    })

     // 获取用户点击影院的影院中的影片详细信息
     const getCinemasFilmList = (async (cinemaId) => {
        const res =   await axios({
            url:`https://m.maizuo.com/gateway/?cinemaId=${cinemaId}&k=9733378`,
            headers:{
                'X-Client-Info':'{"a":"3000","ch":"1002","v":"5.2.1","e":"16923257271850709997780993"}',
                'X-Host':'mall.film-ticket.film.cinema-show-film'
            }
        })
        cinemaFilmLists.value = res.data.data.films
    })

    // 获取用户定位
    const getPostion = (async(latitude,longitude)=>{
      await axios({
            url: `/api/reverse_geocoding/v3/?ak=IBigXlZQVSxxD4lmqNUlrMLGoq8Coi1n&output=json&coordtype=wgs84ll&location=${latitude},${longitude}`,
        }) .then(response => {
            // 从响应中提取城市名称，去掉最后一个字符  长沙市保留长沙
            cityName.value = response.data.result.addressComponent.city.slice(0,-1)
            // 还要设置更改完城市id
            cityList.value.forEach((item)=>{
                if(cityName.value.includes(item.name)) {
                    return cityId.value = item.cityId
                }
            })
            console.log(response.data);
          })
          .catch(error => {
            console.error('逆地理编码失败', error);
          });

       
    })

    const getMainDataList = computed(()=>{
        return mainDataList.value
    })
    const getPageNum = computed(()=>{
         return   pageNum.value++
    })
    
     
    return {
        mainDataList,
        getList,
        getPageNum,
        pageNum,
        getMainDataList,
        total,
        getCinemaList,
        cinemaList,
        ticketFlag,
        bottom,
        cityList,
        getCityList,
        cityId,
        cityName,
        detailList,
        getDetailList,
        finished,
        loading,
        mobile,
        getTrue,
        msg,
        status,
        getSms,
        smsCode,
        dataList,
        getCinamesIds,
        cinemasIds,
        getCinamesIdList,
        cinemasDetailList,
        getDateCinamesIdList,
        filmId,
        getCinemasFilm,
        cinemaId,
        cinemaFilmData,
        getCinemasFilmList,
        cinemaFilmLists,
        getPostion,
        toListOrCinemas
    }
})

export default listStore